package com.web.service;

import com.web.dao.*;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.utils.ChineseName;
import com.web.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LoginService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private DemandClassMapper demandClassMapper;
    @Autowired
    private UserCareenMapper userCareenMapper;
    @Autowired
    private UserCertificationMapper certificationMapper;
    @Autowired
    private UserJobMapper jobMapper;
    @Autowired
    private UserExperienceMapper experienceMapper;
    @Autowired
    private UserCaseMapper caseMapper;
    @Autowired
    private UserMarginMapper marginMapper;
    @Autowired
    private UserDesignerMapper designerMapper;
    @Autowired
    private CurrencyRecordMapper currencyRecordMapper;
    @Autowired
    private UserExpertMapper expertMapper;
    @Autowired
    private UserSupplyMapper supplyMapper;
    @Autowired
    private TechInfoMapper techInfoMapper;
    @Autowired
    private AppSafeMoneyMapper safeMoneyMapper;

    /**
     * 查询用户信息
     * @param key
     * @param value
     * @return
     */
    public UserInfo info(String key,String value){
        return userInfoMapper.findById(key,value);
    }

    /**
     * 更新个人信息
     * @param info
     */
    public void uploadUserInfo(UserInfo info){
        info.setModifyTime(new Date());
        userInfoMapper.updateByPrimaryKeySelective(info);
    }

    /**
     *
     */
    public void update(UserInfo info){
        info.setModifyTime(new Date());
        userInfoMapper.updateByPrimaryKeySelective(info);
    }


    /**
     * 添加用户
     * @param info
     */
    public void addUser(UserInfo info){
        String userId = UUIDUtil.getByUUId();
        info.setNickname(ChineseName.getName());
        info.setUserId(userId);
        info.setJgId(userId);
        info.setCreateTime(new Date());
        info.setModifyTime(new Date());
        info.setDateOfBirth(new Date());
        info.setAvatar("http://img.zcool.cn/community/0117825857a100a801219c778d2554.jpg");
        userInfoMapper.insertSelective(info);
    }

    /**
     * 添加企业信息
     * @param info
     */
    public void addTech(TechInfo info){
        info.setCreateTime(new Date());
        info.setModifyTime(new Date());
        techInfoMapper.insertSelective(info);
    }

    /**
     * 查询企业信息
     * @param userId
     * @return
     */
    public TechInfo selectTechInfo(String userId){
        return techInfoMapper.findTechInfo("user_id",userId);
    }

    /**
     * 获取需求分类列表
     * @return
     */
    public List<DemandClass> allDemand(){
        return demandClassMapper.allDemand();
    }

    /**
     * 获取专业列表
     * @return
     */
    public List<UserCareen> allCareer(){
        return userCareenMapper.allCareer();
    }

    /**
     * 查询用户认证信息
     * @param key
     * @param value
     * @return
     */
    public UserCertification selectCertification(String key,String value){
        return certificationMapper.findUserCertification(key,value);
    }


    /**
     * 实名认证
     * @param certification
     */
    public void addCertification(UserCertification certification){
        certification.setCreateTime(new Date());
        certification.setModifyTime(new Date());
        certificationMapper.insert(certification);
    }

    /**
     * 重新提交
     * @param certification
     */
    public void uploadCertification(UserCertification certification){
        certification.setModifyTime(new Date());
        certificationMapper.updateByPrimaryKeySelective(certification);
    }

    /**
     * 查询用户简历信息
     * @param userId
     * @return
     */
    public UserJob findUserJob(String userId){
        return jobMapper.findUserJob("user_id",userId);
    }

    /**
     * 添加用户简历信息
     * @param job
     */
    public void addUserJob(UserJob job){
        job.setCreateTime(new Date());
        job.setModifyTime(new Date());
        jobMapper.insert(job);
    }
    /**
     * 更新用户简历
     * @param job
     */
    public void modifyJob(UserJob job){
        job.setModifyTime(new Date());
        jobMapper.updateByPrimaryKeySelective(job);
    }

    /**
     * 上传项目经验
     * @param experience
     */
    public void uploadUserExperience(UserExperience experience){

        experienceMapper.insert(experience);
    }

    /**
     * 添加用户案例
     * @param userCase
     */
    public void uploadUserCase(UserCase userCase){
        userCase.setCreateTime(new Date());
        userCase.setModifyTime(new Date());

        caseMapper.insert(userCase);
    }

    /**
     * 获取用户项目经验
     * @param userId
     * @return
     */
    public List<UserExperience> myExperience(String userId){
        return experienceMapper.findByUserExperience("user_id",userId);
    }

    /**
     * 获取用户项目案例
     * @param userId
     * @return
     */
    public List<UserCase> myCase(String userId){
        return caseMapper.findUserCase("user_id",userId);
    }

    /**
     * 获取金额比例表
     * @return
     */
    public UserMargin getMarginInfo(){
        return marginMapper.getMarginInfo();
    }

    /**
     * 获取保证金列表
     * @return
     */
    public List<AppSafeMoney> getSafeMoneyMapper(){
        return safeMoneyMapper.allSafeMoney();
    }



    /**
     * 获取我邀请的好友列表
     * @param invietCode
     * @return
     */
    public List<InviteInfo> myInviteInfo(String invietCode){
        return userInfoMapper.myInviteInfo(invietCode);
    }

    /**
     * 申请设计师
     * @param designer
     */
    public void addDesigner(UserDesigner designer){
        designer.setCreateTime(new Date());
        designer.setModifyTime(new Date());
        designerMapper.insert(designer);
    }

    /**
     * 更新工作室信息
     * @param designer
     */
    public void uploadDesigner(UserDesigner designer){
        designer.setModifyTime(new Date());
        designerMapper.updateByPrimaryKeySelective(designer);
    }

    /**
     * 查询工作室信息
     * @param userId
     * @return
     */
    public UserDesigner findUserDesigner(String userId){
        return designerMapper.findUserDesigner("user_id",userId);
    }

    /**
     * 申请专家
     * @param expert
     */
    public void addExpert(UserExpert expert){
        expert.setCreateTime(new Date());
        expert.setModifyTime(new Date());
        expertMapper.insert(expert);
    }

    /**
     * 更新专家信息
     * @param expert
     */
    public void uploadExpert(UserExpert expert){
        expert.setModifyTime(new Date());
        expertMapper.updateByPrimaryKeySelective(expert);
    }

    /**
     * 查询专家信息
     * @param userId
     * @return
     */
    public UserExpert findUserExpert(String userId){
        return expertMapper.findUserExpert("user_id",userId);
    }

    /**
     * 申请供应商
     * @param supply
     */
    public void addSupply(UserSupply supply){
        supply.setCreateTime(new Date());
        supply.setModifyTime(new Date());
        supplyMapper.insert(supply);
    }

    /**
     * 更新供应商信息
     * @param supply
     */
    public void uploadSupply(UserSupply supply){
        supply.setModifyTime(new Date());
        supplyMapper.updateByPrimaryKeySelective(supply);
    }

    /**
     * 查询供应商信息
     * @param userId
     * @return
     */
    public UserSupply findUserSupply(String userId){
        return supplyMapper.findUserSupply("user_id",userId);
    }

    /**
     * 获取附近的人信息
     * @param longitude1
     * @param longitude2
     * @param latitude1
     * @param latitude2
     * @return
     */
    public List<UserInfo> peopleNearby(String longitude1,String longitude2,String latitude1,String latitude2){
        return userInfoMapper.peopleNearby(longitude1,longitude2,latitude1,latitude2);
    }
    /**
     * 模豆流水记录表
     * @param senderUser
     * @param receiverUser
     * @param currency
     * @param type
     */
    public Response uploadCurrency(String senderUser, String receiverUser, String currency, Integer type){
        UserInfo senderUserInfo = info("user_id",senderUser);
        UserInfo receiverUserInfo = info("user_id",receiverUser);
        //获取保证金列表
        List<AppSafeMoney> safeMoney = getSafeMoneyMapper();
        switch (type){
            case 1:
                if (senderUserInfo.getCurrency() < safeMoney.get(0).getSafemoney()){
                    return Response.custom().failure("模豆余额不足,请先进行充值");
                }
                //扣除前模豆数量
                int currencyBalance = (senderUserInfo.getCurrency());
                //消费模豆数量
                int currencyDeduction = safeMoney.get(0).getSafemoney().intValue();
                senderUserInfo.setCurrency(currencyBalance - currencyDeduction);
                //更新账户信息
                uploadUserInfo(senderUserInfo);
                //添加支出模豆流水记录
                addCurrencyRecord(currencyDeduction,currencyBalance,senderUser,receiverUser,type,senderUserInfo.getIsVip());
                return Response.custom().ok();
            default:
                return Response.custom().failure("使用场景错误!");
        }
    }

    /**
     * 添加模豆流水记录
     * @param currencyDeduction
     * @param currencyBalance
     * @param senderUser
     * @param receiverUser
     * @param useType
     * @param isVip
     */
    public void addCurrencyRecord(int currencyDeduction,int currencyBalance,String senderUser,
                                  String receiverUser,int useType,int isVip){
        CurrencyRecord currencyRecord = new CurrencyRecord();
        currencyRecord.setCreateTime(new Date());
        currencyRecord.setModifyTime(new Date());
        currencyRecord.setCurrencyDeduction(currencyDeduction);
        currencyRecord.setCurrencyBalance(currencyBalance);
        currencyRecord.setUseType(useType);
        currencyRecord.setSender(senderUser);
        currencyRecord.setReceiver(receiverUser);
        currencyRecord.setIsVipExpenditure(isVip);
        currencyRecordMapper.insert(currencyRecord);
    }


    /**
     * 更新企业信息
     * @param techInfo
     */
    public void updateTtechInfo(TechInfo techInfo) {
        techInfo.setModifyTime(new Date());
        techInfoMapper.updateByPrimaryKeySelective(techInfo);
    }
}
